package com.avic.map.common.queue.receive;


import com.alibaba.fastjson.JSONObject;
import com.avic.map.common.constant.CacheConstant;
import com.avic.map.common.constant.LocalResourceConstant;
import com.avic.map.common.constant.MongoConstant;
import com.avic.map.common.model.HistoryPositionModel;
import com.avic.map.common.model.MapCommonModel;
import com.avic.map.common.model.SocketCommonModel;
import com.avic.map.common.model.enums.MapDataStatusEnums;
import com.avic.map.common.model.enums.SocketDataTypeEnums;
import com.avic.map.common.model.enums.SoldierTypeEnums;
import com.avic.map.common.scheduled.MapDataRecoverApplicationRunner;
import com.avic.map.dal.model.ShipStaticInfo;
import com.avic.map.service.CarResourceService;
import com.avic.map.service.MapShipResourceService;
import com.avic.map.service.ShipResourceService;
import com.avic.map.service.ShipStaticInfoService;
import com.avic.map.service.SingleSoldierResourceService;
import com.avic.map.websocket.MapSocketHandler;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.socket.TextMessage;

import javax.annotation.Resource;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


@Slf4j
public abstract class AbstractReceiver {

    /**
     * 线程池
     */
    protected ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(500);

    /**
     * socket 操作类
     * 用户发送消息。接收用户消息并处理
     */
    @Autowired
    protected MapSocketHandler mapSocketHandler;

    /**
     * 地图资源数据service
     */
    @Autowired
    protected MapShipResourceService mapShipResourceService;

    /**
     * 地图资源数据service
     */
    @Autowired
    protected ShipResourceService shipResourceService;

    /**
     * 地图车辆资源数据service
     */
    @Autowired
    protected CarResourceService carResourceService;

    @Autowired
    protected ShipStaticInfoService shipStaticInfoService;

    /**
     * 地图单兵资源数据service
     */
    @Autowired
    protected SingleSoldierResourceService singleSoldierResourceService;

    @Autowired
    protected ObjectMapper objectMapper;

    @Resource(name = CacheConstant.REDIS_BEAN_NAME)
    RedisTemplate redisTemplate;

    @Autowired
    protected MapDataRecoverApplicationRunner mapDataRecoverApplicationRunner;

    @Autowired
    public MongoTemplate mongoTemplate;

    /**
     * 接收到静态数据后更新传播数据名称
     */
    protected void receiverMessageUpdateShipName(String shipNumber, ShipStaticInfo shipStaticInfo) {
        try {
            if (null != shipStaticInfo) {
                if (LocalResourceConstant.SHIP_CURRENT_POSITION_MAP.containsKey(Long.valueOf(shipNumber))) {
                    // 更新缓存
                    this.updateNameOpt(shipNumber, shipStaticInfo);
                    // 校验是否要更新名称
                    if (LocalResourceConstant.VDM_SHIP_STATE_DB_MAP.containsKey(shipNumber)) {
                        String vdmShipName = LocalResourceConstant.VDM_SHIP_STATE_DB_MAP.get(shipNumber).getShipName();
                        if (!StringUtils.equals(vdmShipName, shipStaticInfo.getShipName())) {
                            log.info("船舶 {} 名字发生变化， 名称 【{}】 更新为 【{}】", shipNumber, vdmShipName, shipStaticInfo.getShipName());
                            // 更新数据库
                            mapShipResourceService.updateShipName(shipStaticInfo.getShipName(), shipStaticInfo.getShipNumber());
                        }
                    }
                }
            }
        } catch (Exception e) {
            log.error("更新船舶名称时发生错误：{}", e.getMessage());
        }
    }

    private void updateNameOpt(String shipNumber, ShipStaticInfo shipStaticInfo) throws Exception {
        LocalResourceConstant.VDM_SHIP_STATE_DB_MAP.put(shipNumber, shipStaticInfo);
        MapCommonModel mapCommonModel = LocalResourceConstant.SHIP_CURRENT_POSITION_MAP.get(Long.valueOf(shipNumber));
        mapCommonModel.setModelName(shipStaticInfo.getShipName());
        mapCommonModel.setShipStaticInfo(shipStaticInfo);
        LocalResourceConstant.SHIP_CURRENT_POSITION_MAP.put(Long.valueOf(shipNumber), mapCommonModel);
        //广播消息
        mapSocketHandler.sendMessageToAllUsers(new TextMessage(
                JSONObject.toJSONString(new SocketCommonModel<>(SocketDataTypeEnums.OBJECT_MAP_COMMON_MODEL, mapCommonModel))));
    }

    protected void vdmShipHandler(MapCommonModel newShipMapCommonModel) {
        try {
            long shipMssi = Long.valueOf(newShipMapCommonModel.getModelId());
            // 查看是否存在船舶数据
            if (LocalResourceConstant.SHIP_CURRENT_POSITION_MAP.containsKey(shipMssi)) {
                log.info("本地存在船舶数据： {}, 更新船舶信息", shipMssi);
                // 包含这个船舶的最新坐标信息，判断坐标是否相同
                MapCommonModel oldShipMapCommonModel = LocalResourceConstant.SHIP_CURRENT_POSITION_MAP.get(shipMssi);

                if (oldShipMapCommonModel.getModelIndex() > 7) {
                    // 判断是否要广播消息
                    newShipMapCommonModel.setModelIndex(0);

                    // 航速大于0,并且轨迹不同，替代map数据，广播消息,存入轨迹点
                    boolean positionState = newShipMapCommonModel.getLatitude() != oldShipMapCommonModel.getLatitude()
                            || newShipMapCommonModel.getLongitude() != oldShipMapCommonModel.getLongitude();

                    if (positionState) {
                        log.info("{} 坐标发生变化", shipMssi);
                        // 坐标点不同相同，存入轨迹点，更新传播当前数据
                        newFixedThreadPool.execute(() -> {
                            try {
                                redisTemplate.opsForHash().put(
                                        CacheConstant.REDIS_MAP_CURRENT_SHIP_SPACE,
                                        String.valueOf(shipMssi), objectMapper.writeValueAsString(newShipMapCommonModel));
                            } catch (Exception e) {
                                log.error("更新船舶数据时发生错误 {}", e.getMessage());
                            }
                            try {
                                log.info("{} 存入坐标点", shipMssi);
                                // 存入轨迹点，mongo
                                mongoTemplate.save(new HistoryPositionModel(newShipMapCommonModel),
                                        shipMssi + MongoConstant.SHIP_POSITION_HISTORY_SPACE);
                            } catch (Exception e) {
                                log.error("储存VDM {} 轨迹点时发生错误 ： {}", newShipMapCommonModel.getModelId(), e.getMessage());
                            }
                        });
                    } else {
                        log.info("船舶 {} 坐标未发生变化，或者数据小于0，此时不更新船舶的轨迹数据");
                    }
                    //广播消息
                    mapSocketHandler.sendMessageToAllUsers(new TextMessage(
                            JSONObject.toJSONString(new SocketCommonModel<>(SocketDataTypeEnums.OBJECT_MAP_COMMON_MODEL, newShipMapCommonModel))));

                    LocalResourceConstant.SHIP_CURRENT_POSITION_MAP.put(shipMssi, newShipMapCommonModel);
                } else {
                    oldShipMapCommonModel.setModelIndex(oldShipMapCommonModel.getModelIndex() + 1);
                    LocalResourceConstant.SHIP_CURRENT_POSITION_MAP.put(shipMssi, oldShipMapCommonModel);
                }
            } else {
                log.info("本地缓存中不存在VDM船舶的数据 {}, ", shipMssi);

                ShipStaticInfo shipStaticInfo = shipStaticInfoService.queryShipStaticInfo(newShipMapCommonModel.getModelId());
                if (null != shipStaticInfo) {
                    newShipMapCommonModel.setModelName(shipStaticInfo.getShipName());
                    newShipMapCommonModel.setShipStaticInfo(shipStaticInfo);
                    LocalResourceConstant.VDM_SHIP_STATE_DB_MAP.put(newShipMapCommonModel.getModelId(), shipStaticInfo);
                }
                //广播消息
                mapSocketHandler.sendMessageToAllUsers(new TextMessage(JSONObject.toJSONString(
                        new SocketCommonModel<>(SocketDataTypeEnums.OBJECT_MAP_COMMON_MODEL, newShipMapCommonModel))));
                LocalResourceConstant.SHIP_CURRENT_POSITION_MAP.put(shipMssi, newShipMapCommonModel);

                // 新增船舶数据
                mapShipResourceService.createMapShipResource(newShipMapCommonModel);
                newFixedThreadPool.execute(() -> {
                    try {
                        redisTemplate.opsForHash().put(
                                CacheConstant.REDIS_MAP_CURRENT_SHIP_SPACE, String.valueOf(shipMssi), objectMapper.writeValueAsString(newShipMapCommonModel));
                    } catch (Exception e) {
                        log.error("储存船舶当前坐标Map数据异常 {}", e.getMessage());
                    }
                });
            }
        } catch (Exception e) {
            log.warn("AbstractReceiver类处理VDM船舶的数据时发生错误： {}", e.getMessage());
        }
    }

    protected void shipHandler(MapCommonModel newShipMapCommonModel, double speedOverGround) {
        try {
            long shipMssi = Long.valueOf(newShipMapCommonModel.getModelId());
            // 查看是否存在船舶数据
            if (LocalResourceConstant.SHIP_CURRENT_POSITION_MAP.containsKey(shipMssi)) {
                // 包含这个船舶的最新坐标信息，判断坐标是否相同
                MapCommonModel oldShipMapCommonModel = LocalResourceConstant.SHIP_CURRENT_POSITION_MAP.get(shipMssi);

                if (oldShipMapCommonModel.getModelIndex() > 15) {
                    newShipMapCommonModel.setModelIndex(0);

                    // 航速大于0,并且轨迹不同，替代map数据，广播消息,存入轨迹点
                    boolean positionState = newShipMapCommonModel.getLatitude() != oldShipMapCommonModel.getLatitude()
                            || newShipMapCommonModel.getLongitude() != oldShipMapCommonModel.getLongitude();
                    if (positionState && speedOverGround > 0) {
                        newFixedThreadPool.execute(() -> {
                            try {
                                redisTemplate.opsForHash().put(
                                        CacheConstant.REDIS_MAP_CURRENT_SHIP_SPACE, String.valueOf(shipMssi), objectMapper.writeValueAsString(newShipMapCommonModel));
                            } catch (Exception e) {
                                log.error("更新船舶数据发生错误 {}", e.getMessage());
                            }
                            try {
                                // 存入轨迹点，mongo
                                mongoTemplate.save(new HistoryPositionModel(newShipMapCommonModel),
                                        shipMssi + MongoConstant.SHIP_POSITION_HISTORY_SPACE);
                            } catch (Exception e) {
                                log.error("储存VDO {} 轨迹点时发生错误 ： {}", newShipMapCommonModel.getModelId(), e.getMessage());
                            }
                        });
                    }
                    //广播消息
                    mapSocketHandler.sendMessageToAllUsers(new TextMessage(
                            JSONObject.toJSONString(new SocketCommonModel<>(SocketDataTypeEnums.OBJECT_MAP_COMMON_MODEL, newShipMapCommonModel))));
                    LocalResourceConstant.SHIP_CURRENT_POSITION_MAP.put(shipMssi, newShipMapCommonModel);
                } else {
                    oldShipMapCommonModel.setModelIndex(oldShipMapCommonModel.getModelIndex() + 1);
                    LocalResourceConstant.SHIP_CURRENT_POSITION_MAP.put(shipMssi, oldShipMapCommonModel);
                }
            } else {
                shipResourceService.updateShipState(MapDataStatusEnums.VIDO.getType(), String.valueOf(shipMssi));
                log.info("map不存在数据 {}, ", shipMssi);

                ShipStaticInfo shipStaticInfo = shipStaticInfoService.queryShipStaticInfo(newShipMapCommonModel.getModelId());
                if (null != shipStaticInfo) {
                    newShipMapCommonModel.setModelName(shipStaticInfo.getShipName());
                    newShipMapCommonModel.setShipStaticInfo(shipStaticInfo);
                    LocalResourceConstant.SHIP_STATE_DB_MAP.put(newShipMapCommonModel.getModelId(), shipStaticInfo);
                }
                //广播消息
                mapSocketHandler.sendMessageToAllUsers(new TextMessage(JSONObject.toJSONString(
                        new SocketCommonModel<>(SocketDataTypeEnums.OBJECT_MAP_COMMON_MODEL, newShipMapCommonModel))));
                LocalResourceConstant.SHIP_CURRENT_POSITION_MAP.put(shipMssi, newShipMapCommonModel);
                newFixedThreadPool.execute(() -> {
                    // 新增船舶数据
                    try {
                        redisTemplate.opsForHash().put(
                                CacheConstant.REDIS_MAP_CURRENT_SHIP_SPACE, String.valueOf(shipMssi), objectMapper.writeValueAsString(newShipMapCommonModel));
                    } catch (Exception e) {
                        log.error("储存船舶当前坐标Map数据异常 {}", e.getMessage());
                    }
                });
            }
        } catch (NumberFormatException e) {
            log.warn("AbstractReceiver类处理VDO船舶的数据时发生错误： {}", e.getMessage());
        }
    }

    protected void carHandler(MapCommonModel newGpsMapCommonModel) {
        try {
            String gpsNumber = newGpsMapCommonModel.getModelId();
            // 检查本地静态变量是否存在GPS数据
            if (LocalResourceConstant.CAR_CURRENT_POSITION_MAP.containsKey(gpsNumber)) {
                // 包含这个车辆的最新坐标信息，判断坐标是否相同
                MapCommonModel oldCarMapCommonModel = LocalResourceConstant.CAR_CURRENT_POSITION_MAP.get(gpsNumber);

                // 校验上个船舶信息是否是无信号，如果是改成 在线，如果不是继承上个数据状态
                if (oldCarMapCommonModel.getStatus() == MapDataStatusEnums.CAR_OFFLINE.getType()
                        || oldCarMapCommonModel.getStatus() == MapDataStatusEnums.INVALID.getType()) {
                    LocalResourceConstant.CAR_CURRENT_STATE_MAP.put(gpsNumber, MapDataStatusEnums.CAR_ONLINE.getType());
                    // 更新数据库状态
                    carResourceService.updateCarState(MapDataStatusEnums.CAR_ONLINE.getType(), gpsNumber);
                    newGpsMapCommonModel.setStatus(MapDataStatusEnums.CAR_ONLINE.getType());
                } else if (oldCarMapCommonModel.getStatus() == MapDataStatusEnums.CAR_OFFLINE_EMERGENCY.getType()) {
                    LocalResourceConstant.CAR_CURRENT_STATE_MAP.put(gpsNumber, MapDataStatusEnums.CAR_ONLINE_EMERGENCY.getType());
                    // 更新数据库状态
                    carResourceService.updateCarState(MapDataStatusEnums.CAR_ONLINE_EMERGENCY.getType(), gpsNumber);
                    newGpsMapCommonModel.setStatus(MapDataStatusEnums.CAR_ONLINE_EMERGENCY.getType());
                } else {
                    newGpsMapCommonModel.setStatus(oldCarMapCommonModel.getStatus());
                }

                if (oldCarMapCommonModel.getModelIndex() > 3) {
                    newGpsMapCommonModel.setModelIndex(0);

                    // 航速大于0,并且轨迹不同，替代map数据，广播消息,存入轨迹点
                    boolean positionState = newGpsMapCommonModel.getLatitude() != oldCarMapCommonModel.getLatitude()
                            || oldCarMapCommonModel.getLongitude() != newGpsMapCommonModel.getLongitude();
                    if (positionState) {

                        newFixedThreadPool.execute(() -> {
                            try {
                                redisTemplate.opsForHash().put(
                                        CacheConstant.REDIS_MAP_CURRENT_CAR_SPACE, gpsNumber, objectMapper.writeValueAsString(newGpsMapCommonModel));
                            } catch (Exception e) {
                                log.error("更新redis车辆数据时发生错误： {}", e.getMessage());
                            }
                            try {
                                // 存入轨迹点，mongo
                                mongoTemplate.save(new HistoryPositionModel(newGpsMapCommonModel),
                                        gpsNumber + MongoConstant.CAR_POSITION_HISTORY_SPACE);
                            } catch (Exception e) {
                                log.error("储存车辆 {} 轨迹点时发生错误 ： {}", newGpsMapCommonModel.getModelId(), e.getMessage());
                            }
                        });
                    } else {
                        log.info("车辆 {} 数据的坐标没有发生变化或者速度为0，数据不做处理", gpsNumber);
                    }
                    //广播消息，重新推送一次消息，防止更新时间永远不变
                    String carMessage = JSONObject.toJSONString(new SocketCommonModel<>(SocketDataTypeEnums.OBJECT_MAP_COMMON_MODEL, newGpsMapCommonModel));
                    log.info("推送车辆数据给前端 ：{}", carMessage);
                    mapSocketHandler.sendMessageToAllUsers(new TextMessage(carMessage));
                    //  更新最新点的位置
                    LocalResourceConstant.CAR_CURRENT_POSITION_MAP.put(gpsNumber, newGpsMapCommonModel);
                } else {
                    oldCarMapCommonModel.setModelIndex(oldCarMapCommonModel.getModelIndex() + 1);
                    LocalResourceConstant.CAR_CURRENT_POSITION_MAP.put(gpsNumber, oldCarMapCommonModel);
                }
            } else {
                log.info("本地缓存中，不存在车辆数据 {}, ", gpsNumber);
                newGpsMapCommonModel.setStatus(MapDataStatusEnums.CAR_ONLINE.getType());
                //广播消息
                mapSocketHandler.sendMessageToAllUsers(new TextMessage(JSONObject.toJSONString(
                        new SocketCommonModel<>(SocketDataTypeEnums.OBJECT_MAP_COMMON_MODEL, newGpsMapCommonModel))));
                LocalResourceConstant.CAR_CURRENT_POSITION_MAP.put(gpsNumber, newGpsMapCommonModel);
                LocalResourceConstant.CAR_CURRENT_STATE_MAP.put(gpsNumber, MapDataStatusEnums.CAR_ONLINE.getType());
                newFixedThreadPool.execute(() -> {
                    // 更新车辆数据
                    carResourceService.updateCarState(MapDataStatusEnums.CAR_ONLINE.getType(), gpsNumber);
                    try {
                        redisTemplate.opsForHash().put(
                                CacheConstant.REDIS_MAP_CURRENT_CAR_SPACE, gpsNumber, objectMapper.writeValueAsString(newGpsMapCommonModel));
                    } catch (Exception e) {
                        log.error("储存船舶当前坐标Map数据异常 {}", e.getMessage());
                    }
                });
            }
        } catch (Exception e) {
            log.warn("AbstractReceiver类处理车辆GPS的数据时发生错误： {}", e.getMessage());
        }
    }

    protected void soldierHandler(MapCommonModel newSoldierMapCommonModel, int modelType) {
        try {
            String soldierNumber = newSoldierMapCommonModel.getModelId();
            // 查看是否存在单兵数据
            if (LocalResourceConstant.SOLDIER_CURRENT_POSITION_MAP.containsKey(soldierNumber)) {
                // 包含这个船舶的最新坐标信息，判断坐标是否相同
                MapCommonModel oldMapCommonModel = LocalResourceConstant.SOLDIER_CURRENT_POSITION_MAP.get(soldierNumber);
                // 设置状态
                if (oldMapCommonModel.getStatus() == MapDataStatusEnums.WALKIE_TALKIE_SOLDIER_OFFLINE.getType()) {

                    this.setSoldierState(newSoldierMapCommonModel, soldierNumber, MapDataStatusEnums.WALKIE_TALKIE_SOLDIER_ONLINE);
                } else if (oldMapCommonModel.getStatus() == MapDataStatusEnums.G_TALKIE_SOLDIER_OFFLINE.getType()) {

                    this.setSoldierState(newSoldierMapCommonModel, soldierNumber, MapDataStatusEnums.G_TALKIE_SOLDIER_ONLINE);
                } else if (oldMapCommonModel.getStatus() == MapDataStatusEnums.WALKIE_TALKIE_SOLDIER_OFFLINE_EMERGENCY.getType()) {

                    this.setSoldierState(newSoldierMapCommonModel, soldierNumber, MapDataStatusEnums.WALKIE_TALKIE_SOLDIER_ONLINE_EMERGENCY);
                } else if (oldMapCommonModel.getStatus() == MapDataStatusEnums.G_SOLDIER_OFFLINE_EMERGENCY.getType()) {

                    this.setSoldierState(newSoldierMapCommonModel, soldierNumber, MapDataStatusEnums.G_SOLDIER_ONLINE_EMERGENCY);
                } else {
                    newSoldierMapCommonModel.setStatus(oldMapCommonModel.getStatus());
                }

                // 航速大于0,并且轨迹不同，替代map数据，广播消息,存入轨迹点
                if (oldMapCommonModel.getModelIndex() > 3) {
                    newSoldierMapCommonModel.setModelIndex(0);

                    // 航速大于0,并且轨迹不同，替代map数据，广播消息,存入轨迹点
                    boolean positionState = newSoldierMapCommonModel.getLatitude() != oldMapCommonModel.getLatitude()
                            || newSoldierMapCommonModel.getLongitude() != oldMapCommonModel.getLongitude();
                    if (positionState) {
                        newFixedThreadPool.execute(() -> {
                            try {
                                redisTemplate.opsForHash().put(
                                        CacheConstant.REDIS_MAP_CURRENT_SOLDIER_SPACE, soldierNumber, objectMapper.writeValueAsString(newSoldierMapCommonModel));
                            } catch (Exception e) {
                                log.error("更新4g单兵redis数据时发生错误： {}", e.getMessage());
                            }
                            try {
                                // 存入轨迹点，mongo
                                mongoTemplate.save(new HistoryPositionModel(newSoldierMapCommonModel),
                                        soldierNumber + MongoConstant.SOLIDER_POSITION_HISTORY_SPACE);
                            } catch (Exception e) {
                                log.error("储存单兵 {} 轨迹点时发生错误 ： {}", newSoldierMapCommonModel.getModelId(), e.getMessage());
                            }
                        });
                    }
                    //广播消息
                    String soliderMessage = JSONObject.toJSONString(new SocketCommonModel<>(SocketDataTypeEnums.OBJECT_MAP_COMMON_MODEL, newSoldierMapCommonModel));
                    log.info("推送单兵数据给前端 ：{}", soliderMessage);
                    mapSocketHandler.sendMessageToAllUsers(new TextMessage(
                            JSONObject.toJSONString(new SocketCommonModel<>(SocketDataTypeEnums.OBJECT_MAP_COMMON_MODEL, newSoldierMapCommonModel))));

                    LocalResourceConstant.SOLDIER_CURRENT_POSITION_MAP.put(soldierNumber, newSoldierMapCommonModel);
                } else {
                    oldMapCommonModel.setModelIndex(oldMapCommonModel.getModelIndex() + 1);
                    LocalResourceConstant.SOLDIER_CURRENT_POSITION_MAP.put(soldierNumber, oldMapCommonModel);
                }
            } else {
                log.info("本地缓存里面不存在单兵数据 {}, 该单兵类型为 {}", soldierNumber, modelType);
                int soldierState = MapDataStatusEnums.WALKIE_TALKIE_SOLDIER_ONLINE.getType();
                if (modelType == SoldierTypeEnums.SOLDIER_4G.getType()) {
                    // 4g单兵类型
                    soldierState = MapDataStatusEnums.G_TALKIE_SOLDIER_ONLINE.getType();
                }
                newSoldierMapCommonModel.setStatus(soldierState);
                //广播消息
                mapSocketHandler.sendMessageToAllUsers(new TextMessage(
                        JSONObject.toJSONString(new SocketCommonModel<>(SocketDataTypeEnums.OBJECT_MAP_COMMON_MODEL, newSoldierMapCommonModel))));
                LocalResourceConstant.SOLDIER_CURRENT_POSITION_MAP.put(soldierNumber, newSoldierMapCommonModel);
                LocalResourceConstant.SOLDIER_CURRENT_STATE_MAP.put(soldierNumber, soldierState);
                // 新增船舶数据
                singleSoldierResourceService.updateSoldierState(soldierNumber, soldierState);
                newFixedThreadPool.execute(() -> {
                    try {
                        redisTemplate.opsForHash().put(
                                CacheConstant.REDIS_MAP_CURRENT_SOLDIER_SPACE, soldierNumber, objectMapper.writeValueAsString(newSoldierMapCommonModel));
                    } catch (Exception e) {
                        log.error("储存单兵当前坐标Map数据异常 {}", e.getMessage());
                    }
                });
            }
        } catch (Exception e) {
            log.warn("AbstractReceiver类处理单兵的数据时发生错误： {}", e.getMessage());
        }
    }

    private void setSoldierState(MapCommonModel newSoldierMapCommonModel, String devId, MapDataStatusEnums mapDataStatusEnums) {
        log.info("单兵的数据发生了变化，更新数据库以及其他地方的状态数据");
        // 设置状态
        newSoldierMapCommonModel.setStatus(mapDataStatusEnums.getType());
        LocalResourceConstant.SOLDIER_CURRENT_STATE_MAP.put(devId, mapDataStatusEnums.getType());
        // 更新数据库状态
        singleSoldierResourceService.updateSoldierState(devId, mapDataStatusEnums.getType());
    }
}
